﻿using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.IdentityModel.Tokens.Jwt;
using T.ApiResults;
using T.ApprovalRecord;
using T.Settlement.Domain;
using T.Settlement.Domain.DTO;
using T.Settlement.Read.API.Application.Command.PaymentCommand;

namespace T.Settlement.Read.API.Controllers
{
    /// <summary>
    /// 付款控制器(读)
    /// </summary>
    [Route("api/[controller]/[action]")]
    [Authorize]
    [ApiController]
    public class PaymentController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator _mediator;
        /// <summary>
        /// 构造函数注入
        /// </summary>
        /// <param name="mediator">中介者</param>
        public PaymentController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 显示付款
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<SListDTO<Payment>>> Show([FromQuery]PaymentShowCommand command)
        {
            ApiResult<SListDTO<Payment>> result= new ApiResult<SListDTO<Payment>>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司Id,
                result.Data=await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 返填付款
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Payment>> Backfill([FromQuery] PaymentBackfillCommand command)
        {
            ApiResult<Payment> result = new ApiResult<Payment>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示付款(审批页)
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<SListDTO<Payment>>> ShowPaymentApproval([FromQuery] PaymentApprovalCommand command)
        {
            ApiResult<SListDTO<Payment>> result = new ApiResult<SListDTO<Payment>>();
            try
            {
                command.CompanyID = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司Id,
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 返填付款
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<ApprovalRecordd>>> ApprovalBackfill([FromQuery] PaymentApprovalBackfillCommand command)
        {
            ApiResult<List<ApprovalRecordd>> result = new ApiResult<List<ApprovalRecordd>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
